﻿using System;
using System.Collections.Generic;
using LoungeRepo.Contracts.Normalizer;

namespace LoungeRepo.Normalizer.DataStructures
{
    [Serializable]
    internal class NormalizedObject : INormalizedObject
    {
        [Serializable]
        internal class NormalizedField : INormalizedField
        {
            #region Implementation of INormalizedField
            public string Name { get; set; }
            public object Value { get; set; }
            #endregion
        }

        private readonly string refId;
        private readonly string objectTypename; // storing type as string to be not depended on its defining assembly during deserialization
        private readonly IList<INormalizedField> fields = new List<INormalizedField>();


        [CoverageExclude] protected NormalizedObject() { }
        public NormalizedObject(Type objectType)
        {
            this.refId = Guid.NewGuid().ToString();
            this.objectTypename = objectType.AssemblyQualifiedName;
        }


        public virtual string RefId
        {
            get { return this.refId; }
        }


        public Type ObjectType
        {
            get
            {
                return Type.GetType(this.objectTypename);
            }
        }

        public IEnumerable<INormalizedField> Fields
        {
            get { return this.fields; }
        }

        public void AddField(string name, object value)
        {
            this.fields.Add(new NormalizedField { Name = name, Value = value });
        }
    }


    [Serializable]
    internal class NormalizedObjectReference : INormalizedObjectReference
    {
        public NormalizedObjectReference(INormalizedObject normalizedObject)
        {
            this.RefId = normalizedObject.RefId;
        }

        public String RefId { get; private set; }
    }
}
